﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace UsandoSprites2
{
    class Meteoro : Sprite
    {
        Point area;
        Vector2 vEscala;
        Texture2D textura;
        float profundidade;
        Point frameMeteoro = new Point(1, 1);

        public Meteoro(Texture2D textura, Vector2 posicaoXY, Point frameBounder, Point colisaoBounder, Point frame, Point sheetFrameSize, int updateLastFrameTime, int milisegundosPorFrame, Vector2 velocidade, float profundidade)
        :base(textura,posicaoXY,frameBounder,colisaoBounder,frame,sheetFrameSize,updateLastFrameTime,milisegundosPorFrame,velocidade,profundidade)

        {

        }

        public Meteoro(Point area, Texture2D textura, Vector2 posicaoXY, Point frameBounder, Point colisaoBounder, Point frame, Point sheetFrameSize, int updateLastFrameTime, int milisegundosPorFrame, Vector2 speed, float profundidade)
            : this(textura, posicaoXY, frameBounder, colisaoBounder,
                frame, sheetFrameSize, updateLastFrameTime, milisegundosPorFrame,
                speed, profundidade)
        {
            this.area = area;
            this.textura = textura;

            this.profundidade = profundidade;
           
        }

        private void CalculaNovoMeteoro()
        {
            Random rnd = new Random();
            float escalaXY = rnd.Next(10, 70);
            float escala = (frameBounder.X * escalaXY) / 100;
            vEscala = new Vector2(escala / frameBounder.X, escala / frameBounder.Y);
            posicaoXY.Y = 0 - vEscala.Y * frameBounder.Y;
            float posicaoX = rnd.Next(0, area.X - (int)(frameBounder.X * vEscala.X));
            posicaoXY.X = posicaoX;
        }


        public override Vector2 Direcao
        {
            get
            {

                if (posicaoXY.Y > area.Y)
                {
                    CalculaNovoMeteoro();
                }
                else
                {
                    posicaoXY.Y += velocidade.Y;
                }



                return posicaoXY;
            }
        }

        public override void Update(GameTime gameTime, Rectangle rec)
        {

            posicaoXY = Direcao;
            base.Update(gameTime, rec);

        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {

            spriteBatch.Draw(textura, posicaoXY, new Rectangle(frameMeteoro.X * frameBounder.X, frameMeteoro.Y * frameBounder.Y, frameBounder.X, frameBounder.Y),
                Color.White, 0f, Vector2.Zero, vEscala, SpriteEffects.None, profundidade);

        }

        public override Rectangle bounderSprite
        {
            get
            {
                return new Rectangle((int)posicaoXY.X, (int)posicaoXY.Y, 
                    (int)(vEscala.X * frameBounder.X), (int)((vEscala.Y * frameBounder.Y)));
            }
        }
    }

}

